package edu.hcmus.sow.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.math.util.MathUtils;

public class StringUtil {

   public static String getPrefix(String str) {
      int index = StringUtils.lastIndexOf(str, '.');
      String temp = str.substring(0, index);

      return temp;
   }

   public static String getSuffix(String str) {
      int index = StringUtils.lastIndexOf(str, '.');
      String temp = str.substring(index);

      return temp;
   }

   public static String bytesToString(long bytes) {
      if (bytes <= 1024)
         return "1.0KB";
      String[] _levels = { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
      int index = (int) Math.floor(Math.log(bytes) / Math.log(1024));
      return MathUtils.round((bytes / Math.pow(1024, index)), 2) + _levels[index];
   }
}
